package com.taptech.doufu.ui.view.weex;


import android.graphics.Color;
import androidx.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import com.taobao.weex.layout.measurefunc.TextContentBoxMeasurement;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.utils.WXResourceUtils;
import com.taobao.weex.utils.WXUtils;
import com.taptech.doufu.weex.QLXStringUtil;
import com.taptech.doufu.weex.TFClickableSpan;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by qlx on 2016/12/8.
 */

public class WYTextContentBox extends TextContentBoxMeasurement {
    private String mRangeText = null;
    private int  mRangeColor = -1;
    private Boolean mAtHilighted = false;
    private WXComponent mComponent;

    public WYTextContentBox(WXComponent component) {
        super(component);
        mComponent = component;

    }

    @NonNull
    @Override
    public Spanned createSpanned(String text) {

        SpannableString res = (SpannableString) super.createSpanned(text);
        if ((mRangeText != null) && (res instanceof SpannableString)){

            String[] splits = mRangeText.split("\\|");
            for (String split:splits) {
                _handleTextColor(res,text,split,mRangeColor);
            }
        }

        if (mAtHilighted){
            Pattern pat = Pattern.compile("(@[^@\\s#]{2,})");
            Matcher mat = pat.matcher(text);

            int color = Color.parseColor("#5793d9");
            while (mat.find()) {
                int start = mat.start();
                int end = mat.end();
                String sub = text.substring(start + 1, end);
                res.setSpan(new TFClickableSpan(sub,color),start,end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }
        return  res;
    }

    private void _handleTextColor(SpannableString res , String text, String split ,int color ){
        int[] indexs = QLXStringUtil.indexsOf(text, split);
        if (indexs != null){
            for (int index : indexs){
                res.setSpan(new ForegroundColorSpan(color),index,index + split.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }
    }



    @Override
    public void layoutBefore() {
        this.updateExtraAttr(mComponent.getAttrs());

        super.layoutBefore();
    }


//    @Override
//    protected void updateStyleImp(Map<String, Object> style) {
//
//        if (style instanceof Map){
//            this.updateExtraAttr(style);
//        }
//        super.updateStyleImp(style);
//    }

    @Override
    protected void updateSpannable(Spannable spannable, int spanFlag) {
        super.updateSpannable(spannable, spanFlag);

    }



    public void updateExtraAttr(Map<String, Object> attrs){
        if (attrs instanceof Map){
            Object value =attrs.get("rangeColor");
            if (value != null){
                String color = (value instanceof String) ? (String) value : value.toString();
                mRangeColor = WXResourceUtils.getColor(color);
            }

            Object rangeText = attrs.get("rangeText");
            if (rangeText != null){
                mRangeText = WXUtils.getString(rangeText,null);
            }

            Object at = attrs.get("at");
            if (at != null){
                mAtHilighted = WXUtils.getBoolean(at,false);
            }
        }

    }
}

